﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CommonRepositoryInterface
{
    public interface IRepository<T>
        where T : IDataRecord
    {
        IRepositoryProvider Provider { get; }
        IQueryable<T> Query { get; }
        void Insert(T item);
        void Delete(T item);
        void Update(T item);
    }

    public static class IRepositoryEx
    {
        public static void Upsert<T>(this IRepository<T> repository, T item)
            where T : IDataRecord
        {
            if (item.Id == Database.NewRecordId)
                repository.Insert(item);
            else
                repository.Update(item);
        }

        public static T Id<T>(this IRepository<T> repository, int id)
            where T : IDataRecord
        {
            return (from i in repository.Query
                    where i.Id == id
                    select i).FirstOrDefault();
        }
    }
}
